package com.leo.offer;

import java.util.LinkedList;

/**
 * 用两个队列实现一个栈
 * @author xuexiaolei
 * @version 2018年03月01日
 */
public class T07QueueStack {
    LinkedList queue1 = new LinkedList();
    LinkedList queue2 = new LinkedList();
    public void push(int val){
        if (queue1.isEmpty()){
            queue2.offer(val);
        }else {
            queue1.offer(val);
        }
    }
    public int pop(){
        if (!queue1.isEmpty()){
            while (queue1.size()>1){ queue2.offer(queue1.poll()); }
            return (int) queue1.poll();
        }else if (!queue2.isEmpty()){
            while (queue2.size()>1){ queue1.offer(queue2.poll()); }
            return (int) queue2.poll();
        }else {
            return -1;
        }
    }
}
